package com.example.schoolenterprisealliance.fragment;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.activity.IntroduceActivity;
import com.example.schoolenterprisealliance.adapter.IntroduceAdapter;
import com.example.schoolenterprisealliance.bean.Admin;
import com.example.schoolenterprisealliance.dialog.CertificateDialog;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.service.AdminService;

import java.util.List;
public class IntroduceFragment extends Fragment {
    private IntroduceAdapter introduceAdapter;
    private RecyclerView rv_introduction;
    private List<Admin.DataBean> data;
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_introduce, container, false);
        rv_introduction = view.findViewById(R.id.rv_introduce);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_introduction.setLayoutManager(linearLayoutManager);
        rv_introduction.setOverScrollMode(View.OVER_SCROLL_NEVER);
        adminData();
        return view;
    }
    //添加联盟成员信息
    private void adminData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        AdminService adminService = retrofit.create(AdminService.class);
        //6, 得到Call 的方法
        Call<Admin> call = adminService.getAdminData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Admin>() {
            @Override
            public void onResponse(Call<Admin> call, Response<Admin> response) {
//                获取到的数据
                Admin admin = response.body();
                //数据源
                data = admin.getData();
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getContext().equals("")) {
                        data.get(i).setContext("无描述");
                    }
                    data.get(i).setItemtype(i % 2);
                    Log.e("Admin_response", data.get(i).getContext());
                }
                introduceAdapter = new IntroduceAdapter(data);
                rv_introduction.setAdapter(introduceAdapter);
                introduceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (view.getId()) {
                            case R.id.button_left:
                                Log.d("button", "button_left被点击了");
                                String string1 = data.get(position).getCertificate();
                                CertificateDialog certificateDialog1 = new CertificateDialog(getActivity(),
                                        R.layout.content_certificate_dialog, string1);
                                Window window1 = certificateDialog1.getWindow();
                                certificateDialog1.show();
                                certificateDialog1.setCancelable(false);
                                break;
                            case R.id.button_right:
                                Log.d("button", "button_right被点击了");
                                String string2 = data.get(position).getCertificate();
                                CertificateDialog certificateDialog2 = new CertificateDialog(getActivity(),
                                        R.layout.content_certificate_dialog, string2);
                                Window window2 = certificateDialog2.getWindow();
                                certificateDialog2.show();
                                certificateDialog2.setCancelable(false);
                                break;
                        }
                    }
                });
            }
            @Override
            public void onFailure(Call<Admin> call, Throwable t) {
                Log.e("Adminerror", t.getMessage());
                t.printStackTrace();
            }
        });

    }
}
